import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { getReservations, createReservation, updateReservation, deleteReservation } from '@/api/reservation'
import { Message } from '@arco-design/web-vue'

export const useReservationStore = defineStore('reservation', () => {
  // 状态
  const reservations = ref([])
  const loading = ref(false)
  const pagination = ref({
    current: 1,
    size: 10,
    total: 0
  })
  const filters = ref({
    room_number: '',
    status: '',
    checkin_date: '',
    checkout_date: ''
  })

  // 计算属性
  const statusOptions = computed(() => [
    { label: '待确认', value: 1 },
    { label: '已确认', value: 2 },
    { label: '已入住', value: 3 },
    { label: '已取消', value: 4 }
  ])

  // 获取预订列表
  const fetchReservations = async (params = {}) => {
    loading.value = true
    try {
      const data = await getReservations({
        page: pagination.value.current,
        size: pagination.value.size,
        ...filters.value,
        ...params
      })
      
      console.log('获取到预订数据:', data)
      
      // 后端返回的是 data 数组（不是嵌套在 data.list 中）
      // 并且需要转换字段名
      const rawData = data.data || data.list || data.reservations || []
      
      // 转换字段名以匹配前端期望的格式
      reservations.value = rawData.map(item => ({
        ...item,
        // 保留原字段
        checkin_date: item.check_in || item.checkin_date,
        checkout_date: item.check_out || item.checkout_date,
        total_amount: item.total_price || item.total_amount,
        guest_count: item.guest_count || 1,
        special_requests: item.special_requests || '',
        notes: item.notes || ''
      }))
      
      pagination.value.total = data.total || 0
      pagination.value.current = data.page || data.current_page || 1
      
      console.log('预订列表已更新:', reservations.value.length, '条记录')
    } catch (error) {
      console.error('获取预订列表失败:', error)
      reservations.value = []
    } finally {
      loading.value = false
    }
  }

  // 创建预订
  const createReservationData = async (reservationData) => {
    try {
      await createReservation(reservationData)
      Message.success('预订创建成功')
      await fetchReservations()
      return true
    } catch (error) {
      console.error('创建预订失败:', error)
      return false
    }
  }

  // 更新预订
  const updateReservationData = async (id, reservationData) => {
    try {
      await updateReservation(id, reservationData)
      Message.success('预订更新成功')
      await fetchReservations()
      return true
    } catch (error) {
      console.error('更新预订失败:', error)
      return false
    }
  }

  // 删除预订
  const deleteReservationData = async (id) => {
    try {
      await deleteReservation(id)
      Message.success('预订删除成功')
      await fetchReservations()
      return true
    } catch (error) {
      console.error('删除预订失败:', error)
      return false
    }
  }

  // 设置筛选条件
  const setFilters = (newFilters) => {
    filters.value = { ...filters.value, ...newFilters }
    pagination.value.current = 1
  }

  // 设置分页
  const setPagination = (newPagination) => {
    pagination.value = { ...pagination.value, ...newPagination }
  }

  // 重置筛选条件
  const resetFilters = () => {
    filters.value = {
      room_number: '',
      status: '',
      checkin_date: '',
      checkout_date: ''
    }
    pagination.value.current = 1
  }

  return {
    // 状态
    reservations,
    loading,
    pagination,
    filters,
    
    // 计算属性
    statusOptions,
    
    // 方法
    fetchReservations,
    createReservationData,
    updateReservationData,
    deleteReservationData,
    setFilters,
    setPagination,
    resetFilters
  }
})
